package com.kcwl.carrier.application.service;

import com.google.common.collect.Maps;
import com.kcwl.carrier.domain.entity.RegularRoutesInfo;
import com.kcwl.carrier.domain.service.RegularRoutesInfoDomainService;
import com.kcwl.carrier.infrastructure.constant.CarrierConstant;
import com.kcwl.carrier.infrastructure.enums.CarrierCollectionErrorCodeEnum;
import com.kcwl.carrier.infrastructure.exceptions.CarrierCollectionException;
import com.kcwl.carrier.interfaces.command.RegularRoutesInfoCommand;
import com.kcwl.carrier.interfaces.dto.RegularRoutesInfoDTO;
import com.kcwl.carrier.interfaces.query.RegularRoutesInfoQuery;
import com.kcwl.framework.utils.KcBeanConverter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @描述 收藏线路
 * @创建人 zsl
 * @创建时间 2023/01/07
 */
@Service
public class RegularRoutesInfoService {

    @Autowired
    private RegularRoutesInfoDomainService regularRoutesInfoDomainService;

    public Map<String, Integer> addRoute(RegularRoutesInfoCommand command) {
        RegularRoutesInfo regularRoutesInfo = KcBeanConverter.toBean(command, RegularRoutesInfo.class);
        RegularRoutesInfo regularRoutesInfoQuery = new RegularRoutesInfo();
        regularRoutesInfoQuery.setUserId(command.getUserId());
        List<RegularRoutesInfo> regularRoutesInfoList = regularRoutesInfoDomainService.listRoute(regularRoutesInfoQuery);
        if (regularRoutesInfoList.size() >= CarrierConstant.REGULAR_ROUTES_COUNT) {
            throw new CarrierCollectionException(CarrierCollectionErrorCodeEnum.REGULAR_ROUTES_COUNT, "最多收藏" + CarrierConstant.REGULAR_ROUTES_COUNT + "条线路");
        }
        regularRoutesInfoList = regularRoutesInfoDomainService.listRoute(regularRoutesInfo);
        if (regularRoutesInfoList.size() > 0) {
            throw new CarrierCollectionException(CarrierCollectionErrorCodeEnum.REGULAR_ROUTES_REPETITION, "您添加该路线已存在");
        }
        Integer upCount = regularRoutesInfoDomainService.addRoute(regularRoutesInfo);
        Map<String, Integer> result = Maps.newHashMap();
        result.put("success", upCount);
        return result;
    }

    public Map<String, Integer> updateRoute(RegularRoutesInfoCommand command) {
        RegularRoutesInfo regularRoutesInfo = KcBeanConverter.toBean(command, RegularRoutesInfo.class);
        List<RegularRoutesInfo> regularRoutesInfoList = regularRoutesInfoDomainService.listRoute(regularRoutesInfo);
        if (regularRoutesInfoList.size() > 0) {
            throw new CarrierCollectionException(CarrierCollectionErrorCodeEnum.REGULAR_ROUTES_REPETITION, "您更改后线路已存在");
        }
        //线路选全国 特殊处理
        if ("000000".equals(regularRoutesInfo.getStartProvinceCode())) {
            regularRoutesInfo.setStartCity("");
            regularRoutesInfo.setStartCityCode("");
            regularRoutesInfo.setStartCounty("");
            regularRoutesInfo.setStartCountyCode("");
        }
        if ("000000".equals(regularRoutesInfo.getTargetProvinceCode())) {
            regularRoutesInfo.setTargetCity("");
            regularRoutesInfo.setTargetCityCode("");
            regularRoutesInfo.setTargetCounty("");
            regularRoutesInfo.setTargetCountyCode("");
        }
        Integer upCount = regularRoutesInfoDomainService.updateRoute(regularRoutesInfo);
        Map<String, Integer> result = Maps.newHashMap();
        result.put("success", upCount);
        return result;
    }

    public Map<String, Integer> delRoute(RegularRoutesInfoCommand command) {
        RegularRoutesInfo regularRoutesInfo = KcBeanConverter.toBean(command, RegularRoutesInfo.class);
        Integer upCount = regularRoutesInfoDomainService.delRoute(regularRoutesInfo);
        Map<String, Integer> result = Maps.newHashMap();
        result.put("success", upCount);
        return result;
    }

    public List<RegularRoutesInfoDTO> listRoute(RegularRoutesInfoQuery query) {
        if (Objects.isNull(query.getUserId())) {
            throw new CarrierCollectionException(CarrierCollectionErrorCodeEnum.USER_ID_NOT_NULL);
        }
        RegularRoutesInfo regularRoutesInfo = KcBeanConverter.toBean(query, RegularRoutesInfo.class);
        List<RegularRoutesInfo> list = regularRoutesInfoDomainService.listRoute(regularRoutesInfo);

        List<RegularRoutesInfoDTO> result = new ArrayList<>();
        Optional.ofNullable(list).ifPresent(l -> result.addAll(l.stream().map(item -> {
            RegularRoutesInfoDTO info = KcBeanConverter.toBean(item, RegularRoutesInfoDTO.class);
            info.setStartAddress(StringUtils.join(item.getStartProvince(), item.getStartCity(), item.getStartCounty()));
            info.setTargetAddress(StringUtils.join(item.getTargetProvince(), item.getTargetCity(), item.getTargetCounty()));
            return info;
        }).collect(Collectors.toList())));
        return result;
    }
}
